package main

import (
	"fmt"
	"encoding/gob"
	"net"
)



func main(){
	go server()
	go client()
	var input string 
	fmt.Scan(&input)
}


//服务端
func server(){
	ln,err := net.Listen("tcp",":8080")

	if nil != err {
		print(err)
		return
	}

	for{
		con,err :=	ln.Accept()
		if nil != err {
			print(err)
			continue
		}
		go handleServerConnection(con)

	}
}

func handleServerConnection(c net.Conn){
	var msg  string
	err := gob.NewDecoder(c).Decode(&msg)
	if nil != err {
		fmt.Println(err)
	}else{
		fmt.Println(err)
	}
	c.Close()
}

//客户端
func client(){
	c,err := net.Dial("tcp","127.0.0.1:8080")
	if err != nil {
		fmt.Println(err)
		return
	}
	msg :=  "Hello World"
	fmt.Println(msg)
	err = gob.NewEncoder(c).Encode(msg)
	if err != nil {
		fmt.Println(err)
		return
	}
	c.Close()
}

